<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script type="text/javascript" src="../../js/jquery-3.2.1.js"></script>
    <script type="text/javascript" src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
  </head>
  
  <body>
	<div class="x-body">
		<form class="layui-form" lay-filter="Textform">
			<div class="layui-form-item" style="width:300px;">
				<label for="name" class="layui-form-label"> <span
					class="x-red">*</span>设施类型</label>
				<div class="layui-input-block" id = "view">
					<select id="selecttype" name="communityType" lay-filter="choosetype" 
					required="" lay-verify="required" autocomplete="off">
					<option value="商铺" >商铺</option>
					<option value="运动场" >运动场</option>
					<option value="绿化" >绿化</option>
					<option value="广场" >广场</option>
					<option value="公共用地" >公共用地</option>
					<option value="社区管理设备" >社区管理设备</option>
					<option value="其他" >其他</option>
	  				</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label for="name" class="layui-form-label"> <span
					class="x-red">*</span>设施名称
				</label>
				<div class="layui-input-inline">
					<input type="text" id="communityName" name="communityName"
						required="" lay-verify="required" autocomplete="off"
						class="layui-input">
				</div>
			</div>
			<div class="layui-form-item" style="width:300px;">
				<label class="layui-form-label">所属小区</label>
				<div class="layui-input-block" id = "view">
					<select id="selectvillage" name="blgVillageId" lay-filter="choosevillage">
					<option value="0" >请选择</option>
					
	  				</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"> 业主账号 </label>
				<div class="layui-input-inline">
					<input type="text" id="username" name="username" required=""
						autocomplete="off" class="layui-input">
				</div>
				<button id="check" class="layui-btn layui-btn-normal">验证</button>
			</div>
			<div class="layui-form-item" id="userCheck" data-id="" style="height:50px;">
			<label class="layui-form-label" style="height:50px;"> 验证信息 </label>
				<label><span id="Istrue"></span> </label></br>
				<label> <span id="presonName"> </span> </label></br>
				<label> <span id="userId"> </span> </label>
				
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span id="blgUserId" name="blgUserId"> </span></label>
			</div>
			<div class="layui-form-item">
				<label for="L_repass" class="layui-form-label"> </label>
				<button class="layui-btn" lay-filter="add" lay-submit="">
					增加</button>
			</div>
		</form>
	</div>
	<script>
	layui.use(['form','layer'], function(){
        var form = layui.form
        ,layer = layui.layer;
        
        //使用全局变量
        var hasUser = false;
      
        $(function(){
        	
        	//加载小区的下拉菜单
        	$.ajax({
        		type: "post",
              	url: "../../Information/showallvillage",
              	async: true,
              	success:function(result){
              		
              		
              		$.each(result.listvillage,function(index,value){
              			//下列2种方法其中一种都可以
              		    $("#selectvillage").append("<option value="+value.villageId+">"+value.villageName+"</option>");
              		    form.render();
              		});
              	},
              	error:function(){
              		layer.msg("小区的信息没有正确地加载到选择菜单中!",{icon:6,time:1500});
              	}
              	
        	});
        	
        	$("#check").bind("click",function(){
        		
        		console.log("ok");
        		var userName = $("#username").val();
            	
            	$.ajax({
            		type:"post",
            		url:"../../Information/getUserInfoByUserName",
            		async: true,
            		data:{
            			"userName":userName
            		},
            		success:function(result){
            			if(result.user.userId!=null){
            				if(result.user.presonName==null){result.user.presonName=""};
            				$("#presonName").text("用户的姓名："+result.user.presonName);
            				$("#userId").text("用户ID："+result.user.userId);
            				//在div中加入data-id
            				$("#userCheck").attr("data-id",result.user.userId);
            				$("#Istrue").text("该用户可以绑定");
            				$("#Istrue").css('color','#00FF00');
            				hasUser = true;
            			}else{
            				$("#presonName").text("");
            				$("#userId").text("");
            				$("#Istrue").text("没有查找到该用户");
            				$("#Istrue").css('color','#FF0000');
            				hasUser = false;
            			}
            		},
            		error:function(){
            			layer.msg("异常的查询!",{icon:6,time:1500});
            		}
            	});
            	
            	//这也是layui的大坑之一，放在表单中的按钮会自动提交，加上这个就禁用了该按钮的跳转
            	return false;
        	});
        	
        });
        
        
        //监听提交
        form.on('submit(add)', function(data){
        
        var tips = "你还没有为社区设施绑定用户，是否继续提交？"
        if(hasUser!=false){tips = "用户已绑定，继续提交？"}
        
        layer.confirm(tips,function(){
        	
        
          var json={};
          json.communityType=$("#selecttype").val();
          json.communityName=$("#communityName").val();
          json.blgVillageId=$("#selectvillage").val();
          if(hasUser!=false){
        	  json.blgUserId=$("#userCheck").attr("data-id");
          }else{
        	  json.blgUserId=null;
          }
          
          
          $.ajax({
          	type: "post",
          	url: "../../Information/addcommunity",
          	async: true,
          	data:json,
          	dataType:'json',
          	success: function(result) {
          		if(result.msg == 'success') {
            			layer.msg("添加成功",{icon:6,time:1500},function(){
            				var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            				parent.layer.close(index); //再执行关闭   
            				parent.location.reload();
                		});
          		} else {
          			layer.msg("添加失败",{icon:5,time:1500},function(){
              			
            			});
          		} 
          	},   	
          	error: function() {
                  layer.alert("数据异常", {icon: 2,time:1500});
              }
          });
        });
          return false;
        });
      });
	</script>

  </body>

</html>